package com.ruoyi.system.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.ArrayList;
import com.ruoyi.common.utils.StringUtils;
import org.springframework.transaction.annotation.Transactional;
import com.ruoyi.system.domain.PowerTreasure;
import com.ruoyi.system.mapper.PowerStationMapper;
import com.ruoyi.system.domain.PowerStation;
import com.ruoyi.system.service.IPowerStationService;

/**
 * 站点Service业务层处理
 * 
 * @author ruoyi
 * @date 2025-03-21
 */
@Service
public class PowerStationServiceImpl implements IPowerStationService 
{
    @Autowired
    private PowerStationMapper powerStationMapper;

    /**
     * 查询站点
     * 
     * @param id 站点主键
     * @return 站点
     */
    @Override
    public PowerStation selectPowerStationById(Long id)
    {
        return powerStationMapper.selectPowerStationById(id);
    }

    /**
     * 查询站点列表
     * 
     * @param powerStation 站点
     * @return 站点
     */
    @Override
    public List<PowerStation> selectPowerStationList(PowerStation powerStation)
    {
        return powerStationMapper.selectPowerStationList(powerStation);
    }

    /**
     * 新增站点
     * 
     * @param powerStation 站点
     * @return 结果
     */
    @Transactional
    @Override
    public int insertPowerStation(PowerStation powerStation)
    {
        powerStation.setCreateTime(DateUtils.getNowDate());
        int rows = powerStationMapper.insertPowerStation(powerStation);
        insertPowerTreasure(powerStation);
        return rows;
    }

    /**
     * 修改站点
     * 
     * @param powerStation 站点
     * @return 结果
     */
    @Transactional
    @Override
    public int updatePowerStation(PowerStation powerStation)
    {
        powerStation.setUpdateTime(DateUtils.getNowDate());
        powerStationMapper.deletePowerTreasureByStationId(powerStation.getId());
        insertPowerTreasure(powerStation);
        return powerStationMapper.updatePowerStation(powerStation);
    }

    /**
     * 批量删除站点
     * 
     * @param ids 需要删除的站点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePowerStationByIds(Long[] ids)
    {
        powerStationMapper.deletePowerTreasureByStationIds(ids);
        return powerStationMapper.deletePowerStationByIds(ids);
    }

    /**
     * 删除站点信息
     * 
     * @param id 站点主键
     * @return 结果
     */
    @Transactional
    @Override
    public int deletePowerStationById(Long id)
    {
        powerStationMapper.deletePowerTreasureByStationId(id);
        return powerStationMapper.deletePowerStationById(id);
    }

    /**
     * 新增充电宝信息
     * 
     * @param powerStation 站点对象
     */
    public void insertPowerTreasure(PowerStation powerStation)
    {
        List<PowerTreasure> powerTreasureList = powerStation.getPowerTreasureList();
        Long id = powerStation.getId();
        if (StringUtils.isNotNull(powerTreasureList))
        {
            List<PowerTreasure> list = new ArrayList<PowerTreasure>();
            for (PowerTreasure powerTreasure : powerTreasureList)
            {
                powerTreasure.setStationId(id);
                list.add(powerTreasure);
            }
            if (list.size() > 0)
            {
                powerStationMapper.batchPowerTreasure(list);
            }
        }
    }
}
